package com.tf_cut.backend.seller.service;

import java.sql.Timestamp;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tf_cut.backend.platform.pojo.GrantRefund;
import com.tf_cut.backend.platform.service.GrantRefundService;
import com.tf_cut.backend.platform.vo.GrantRefundVo;
import com.tf_cut.backend.request.BkcloudfundsRefundNotifyRequest;
import com.tf_cut.backend.request.CommomRequstHandle;
import com.tf_cut.backend.request.MybankNotifyService;
import com.tf_cut.backend.request.xml.XmlConverter;
import com.tf_cut.backend.seller.vo.RefundNotifyModel;
import com.tf_cut.backend.util.RequestHead;

@Service(value = "bkcloudfundsRefundNotifyService")
public class RefundNotifyImpl implements MybankNotifyService{
	
    @Autowired
    private CommomRequstHandle commomRequstHandle;
    
    @Autowired
    private GrantRefundService grantRefundService;

    public String notifyHandle(String xmlContext, String function) throws Exception {
        //通知内容转换
    	BkcloudfundsRefundNotifyRequest refundNotifyRequest =
                XmlConverter.getInstance().toResponse(xmlContext, BkcloudfundsRefundNotifyRequest.class);
        RequestHead head = refundNotifyRequest.getBkcloudfundsRefundNotify().getRequestHead();
        //保存通知内容
        boolean flag = saveNotify(refundNotifyRequest.getBkcloudfundsRefundNotify().getBkcloudfundsRefundNotifyModel());
        //响应结果根据执行结果统一处理
        return commomRequstHandle.getSignResult(flag, head);
    }

    private boolean saveNotify(RefundNotifyModel mobel) {
    	GrantRefundVo grantRefund = new GrantRefundVo();
    	grantRefund.setId(UUID.randomUUID().toString().replace("-", ""));
    	grantRefund.setIsvOrgId(mobel.getIsvOrgId());
    	grantRefund.setParticipantType(mobel.getParticipantType());
    	grantRefund.setParticipantId(mobel.getParticipantId());
    	grantRefund.setRelateOrderNo(mobel.getRelateOrderNo());
    	grantRefund.setRelateTransNo(mobel.getRelateTransNo());
    	grantRefund.setRefundOrderNo(mobel.getRefundOrderNo());
    	grantRefund.setChannelRefundOrderNo(mobel.getChannelRefundOrderNo());
    	grantRefund.setOutRefundNo(mobel.getOutRefundNo());
    	grantRefund.setRefundAmount(mobel.getRefundAmount());
    	grantRefund.setCurrency(mobel.getCurrency());
    	grantRefund.setStatus(mobel.getStatus());
    	grantRefund.setParticipantActualRefundAmount(mobel.getParticipantActualRefundAmount());
    	grantRefund.setRefundFinishDate(mobel.getRefundFinishDate());
    	grantRefund.setErrorCode(mobel.getErrorCode());
    	grantRefund.setErrorDesc(mobel.getErrorDesc());
    	grantRefund.setExtInfo(mobel.getExtInfo());
    	grantRefund.setMemo(mobel.getMemo());
    	grantRefund.setAskTime(new Timestamp(System.currentTimeMillis()));
    	grantRefundService.save(grantRefund);
        return true;
    }

}
